package org.csu.small.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.csu.small.common.Constant;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.Category;
import org.csu.small.entity.Product;
import org.csu.small.service.IProductService;
import org.csu.small.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/manage/product/")
@Api(value = "API - AProductController", description = "后台商品模块接口详情")
public class AProductController {

    @Autowired
    private IProductService productService;

    @PostMapping("add_product")
    @ResponseBody
    @ApiOperation(value = "新增商品")
    public UniformResponse<String> addProduct(Product product){

        return productService.addProduct(product);
    }

    @GetMapping("detail")
    @ResponseBody
    @ApiOperation(value = "商品详情")
    public UniformResponse<ProductVO> detail(Integer productId){
        UniformResponse<ProductVO> response = productService.detail(productId, Constant.Role.ADMIN);

        return response;
    }

    @PostMapping("edit_product_info")
    @ResponseBody
    @ApiOperation(value = "修改商品信息")
    public UniformResponse<String> edit_product_info(Product product){

        return productService.edit_product_info(product);
    }

    @PostMapping("upload")
    @ResponseBody
    @ApiOperation(value = "上传图片")
    public UniformResponse<String> product_upload(HttpServletRequest request, MultipartFile upload_file){
        String path = request.getSession().getServletContext().getRealPath("upload");
        return productService.product_upload(upload_file, path);
    }

    @PostMapping("richtext_img_upload.do")
    @ResponseBody
    @ApiOperation(value = "上传图片")
    public UniformResponse<String> richtext_img_upload(HttpServletRequest request, MultipartFile upload_file){
        String path = request.getSession().getServletContext().getRealPath("upload");
        return productService.richtext_img_upload(upload_file, path);
    }

    @GetMapping("set_sale_status")
    @ResponseBody
    @ApiOperation(value = "修改商品状态")
    public UniformResponse<String> set_sale_status(Integer productId, Integer status){

        return productService.set_sale_status(productId, status);
    }

    @GetMapping("search")
    @ResponseBody
    @ApiOperation(value = "后台查询商品")
    public UniformResponse<Object> search(
            @RequestParam(name = "id", required = false) Integer id,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize){

        return productService.search(id, keyword, pageNum, pageSize);
    }

    @GetMapping("list")
    @ResponseBody
    @ApiOperation(value = "商品查询")
    public UniformResponse<Object> getProductList(
            @RequestParam(name = "categoryId", required = false) Integer categoryId,
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(name = "orderBy", defaultValue = "") String orderBy){
        UniformResponse<Object> response = productService.getProductList(categoryId, keyword, pageNum, pageSize, orderBy, Constant.Role.ADMIN);

        return response;
    }
}
